package com.pan.design.pattern.behavioral.interpreter;

/**
 * 解释器模式
 * Created by geely.
 */
public class Test {

//    public static void main(String[] args) {
//        String geelyInputStr="6 100 11 + *";
//        GeelyExpressionParser expressionParser=new GeelyExpressionParser();
//        int result=expressionParser.parse(geelyInputStr);
//        System.out.println("解释器计算结果: "+result);
//
//    }

    public static void main(String[] args) {

        int all = calall(5,1,1);
        System.out.println("all:"+all);

        int[] arr = new int[]{2,5,4,7,88,98,3,44,56};
        mapaoSrot(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }




    public static int calall(int np,int nq,int nr){
        int[][][][] memo = new int[3][np+1][nq+1][nr+1];
        for (int i = 0; i < memo.length; i++) {
            for (int i1 = 0; i1 < memo[i].length; i1++) {
                for (int i2 = 0; i2 < memo[i][i1].length; i2++) {
                    for (int i3 = 0; i3 < memo[i][i1][i2].length; i3++) {
                        memo[i][i1][i2][i3]=-1;
                    }
                }
            }
        }
        System.out.println(memo[0][0][0][0]);

        int pball = pBall(np,nq,nr,memo);
        int qball = qBall(np,nq,nr,memo);
        int rball = rBall(np,nq,nr,memo);
        System.out.println("pBall:"+pball+",qball:"+qball+",rball:"+rball);
        int all = pball+qball+rball;
        return all;
    }

    //f(n) = 1+f(n-1)
    public static int pBall(int np,int nq,int nr,int[][][][] memo) {

        if(np<=0){
            memo[0][0][nq][nr] =0;
            return 0;
        }
        if(np==1&&nq==0&&nr==0){
            memo[0][1][nq][nr] =1;
            return 1;
        }
        if(memo[0][np][nq][nr]==-1){
            memo[0][np][nq][nr] = qBall(np-1,nq,nr,memo)+rBall(np-1,nq,nr,memo);
            System.out.println("memo[0]["+np+"]["+nq+"]["+nr+"]:"+memo[0][np][nq][nr]);
        }
        return memo[0][np][nq][nr];
    }


    public static int qBall(int np,int nq,int nr,int[][][][] memo) {

        if(nq<=0){
            memo[1][np][0][nr] =0;
            return 0;
        }
        if(nq==1&&np==0&&nr==0){
            memo[1][np][1][nr] =1;
            return 1;
        }
        if(memo[1][np][nq][nr]==-1){
            memo[1][np][nq][nr] = pBall(np,nq-1,nr,memo)+rBall(np,nq-1,nr,memo);
            System.out.println("memo[1]["+np+"]["+nq+"]["+nr+"]:"+memo[1][np][nq][nr]);
        }

        return memo[1][np][nq][nr];
    }


    public static int rBall(int np,int nq,int nr,int[][][][] memo) {

        if(nr<=0){
            memo[1][np][nq][0] =0;
            return 0;
        }
        if(nr==1&&np==0&&nq==0){
            memo[1][np][nq][1] =1;
            return 1;
        }
        if(memo[2][np][nq][nr]==-1){
            memo[2][np][nq][nr]= pBall(np,nq,nr-1,memo)+qBall(np,nq,nr-1,memo);
            System.out.println("memo[2]["+np+"]["+nq+"]["+nr+"]:"+memo[2][np][nq][nr]);
        }
        return memo[2][np][nq][nr];
    }


    public static int[]  mapaoSrot(int[] arr){

        for (int i = 0; i < arr.length; i++) {
            for(int j=i;j<arr.length;j++){
                if(arr[i]>arr[j]){
                    int temp = arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                }
            }
        }
        return arr;
    }

}
